package codingforgreat.lchot150;

import java.util.Stack;

public class Lc150 {
    public int evalRPN(String[] tokens) {
        Stack<Integer> stack = new Stack<>();
        for(String s : tokens){
            if(s.equals("+") || s.equals("-") || s.equals("*") || s.equals("/")){
                compose(stack,s);
            }else{
                stack.push(Integer.valueOf(s));
            }
        }
        return stack.peek();
    }
    public void compose(Stack<Integer> stack,String s){
        Integer a = stack.pop();
        Integer b = stack.pop();
        int res = 0;
        switch (s) {
            case "+" :
                res =  a + b;
                break;
            case"-" :
                res = b - a;
                break;
            case"*" :
                res = a * b;
                break;
            case"/":
                res = b/a;
                break;
        }
        stack.push(res);
    }
}
